package com.atguigu.designpattern.singleton.type2;

public class SingletonTest02 {

    public static void main(String[] args) {
        System.out.println("单例模式-饿汉式");
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();

        System.out.println(singleton1 == singleton2);
        System.out.println("singleton1 hashcode" + singleton1.hashCode());
        System.out.println("singleton2 hashcode" + singleton2.hashCode());

    }

}

class Singleton {

    //1、构造器私有化，外部不能new
    private Singleton() {

    }

    //2、本类内部创建对象实例
    private final static Singleton instance;

    static { //在静态代码块中初始化实例
        instance = new Singleton();
    }


    //3、提供一个公共静态方法，返回对象实例
    public static Singleton getInstance() {
        return instance;
    }
}
